<template>
	<view class="u-wrap">
		<u-navbar title="筛选"></u-navbar>
		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop"
				:style="{height:menuHeight+'px'}" :scroll-into-view="itemId">
				<view v-for="(item,index) in tabbar" :key="index" class="u-tab-item"
					:class="[current == index ? 'u-tab-item-active' : '']" @tap.stop="swichMenu(index)">
					<text class="u-line-1">{{item.name}}</text>
				</view>
			</scroll-view>
			<scroll-view :scroll-top="scrollRightTop" scroll-y scroll-with-animation class="right-box"
				:style="{height:menuHeight+'px'}" @scroll="rightScroll">
				<view class="page-view">
					<view class="class-item" :id="'item' + index" v-for="(item , index) in tabbar" :key="index">
						<view class="item-title">
							<text>{{item.name}}</text>
						</view>
						<view class="item-container">
							<view class="thumb-box"
								:class="[item1.check && index1 == 0 ? 'thumb-box--active-all' : '',item1.check ? 'thumb-box--active' : '']"
								v-for="(item1, index1) in item.list" :key="index1"
								@click="onClick(item1,index1,index,item)">
								<view class="item-menu-name ">{{item1.name}}</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>

		<u-bottom-button :show-cancel-button="true" cancelText="清除" confirmText="确认" @cancel='onRemove'
			@confirm="onSave"></u-bottom-button>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				utype: uni.getStorageSync('utype'),
				isRightScroll: false, // 点击左侧时禁止右侧滚动导致左侧重新赋值
				scrollTop: 0, //tab标题的滚动条位置
				oldScrollTop: 0,
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				itemId: '', // 栏目右边scroll-view用于滚动的id
				tabbar: [],
				menuItemPos: [],
				arr: [],
				scrollRightTop: 0, // 右边栏目scroll-view的滚动条高度
				timer: null, // 定时器
			}
		},
		mounted() {
			this.menuHeight = uni.$u.sys().screenHeight - uni.$u.sys().statusBarHeight - 106
		},
		onLoad({
			item
		}) {
			if (item != '[]') {
				this.tabbar = JSON.parse(item)
			} else {
				this.getSearchConfig()
			}
		},
		methods: {
			// 保存当前已选中数据
			onSave() {
				uni.$emit('screen', this.tabbar)
				uni.$u.route({
					type: 'back'
				})
			},
			// 清除所有已选中状态
			onRemove() {
				this.tabbar.map(item => {
					item.list = item.list.map((item1, index1) => {
						if (index1 == 0) {
							item1.check = true
						} else {
							item1.check = false
						}
						return item1
					})
					return item
				})
			},
			// 点击右侧按钮选中
			onClick(item, index, pIndex, {
				name
			}) {
				let list = this.tabbar[pIndex].list.slice(1)
				//  年龄  更新时间  性别要求 薪资待遇 年龄要求 单选  其他多选
				if (name == '年龄要求' || name == '更新时间' || name == '性别要求' || name == '年龄要求' || name == '薪资待遇') {
					// 判断当前是否是已选中状态
					if (item.check) {
						this.$set(this.tabbar[pIndex].list[index], 'check', false)
					} else {
						// 先清空所有的已选中状态
						this.tabbar[pIndex].list.map((item1, index) => {
							if (index != 0) {
								item1.check = false
							}
							return item1
						})
						this.$set(this.tabbar[pIndex].list[index], 'check', true)
					}
					// 判断当前板块是否有已选中的 无则选中不限
					let cur = list.findIndex(item1 => item1.check)
					if (cur == -1) {
						this.tabbar[pIndex].list[0].check = true
					} else {
						this.tabbar[pIndex].list[0].check = false
					}
				} else {
					if (item.name == '不限') {
						this.tabbar[pIndex].list[0].check = true
						this.tabbar[pIndex].list.forEach(item => {
							if (item.name == '不限') return
							item.check = false
						})
					} else {
						item.check = !item.check
						this.tabbar[pIndex].list[0].check = false
						let cur = list.findIndex(item1 => item1.check)
						if (cur == -1) {
							this.tabbar[pIndex].list[0].check = true
						} else {
							this.tabbar[pIndex].list[0].check = false
						}
					}

				}

			},
			// 获取筛选界面数据
			async getSearchConfig() {
				if (this.utype == 1) {
					this.tabbar = [{
						name: '性别要求'
					}, {
						name: '年龄要求'
					}, {
						name: '薪资待遇'
					}, {
						name: '简历标签'
					}, {
						name: '更新时间'
					}]
				} else {
					this.tabbar = [{
						name: '经验要求'
					}, {
						name: '学历要求'
					}, {
						name: '年龄要求'
					}, {
						name: '简历标签'
					}, {
						name: '更新时间'
					}]
				}
				let params = {
					type: 'experience,education,resumeTag'
				}
				let res = await uni.$u.api.getClassifyInfo({
					params
				})
				let {
					education,
					experience,
					resumeTag
				} = res
				let age = [{
					id: 0,
					name: '不限',
					min: 0,
					max: 0
				}, {
					id: 1,
					name: '16-20岁',
					min: 16,
					max: 20
				}, {
					id: 2,
					name: '20-30岁',
					min: 20,
					max: 30
				}, {
					id: 3,
					name: '30-40岁',
					min: 30,
					max: 40
				}, {
					id: 4,
					name: '40-50岁',
					min: 40,
					max: 50
				}, {
					id: 5,
					name: '50岁以上',
					min: 50,
					max: 70
				}]


				let updateTime = [{
					id: 0,
					name: '不限'
				}, {
					id: 3,
					name: '3天内'
				}, {
					id: 7,
					name: '7天内'
				}, {
					id: 15,
					name: '15天内'
				}, {
					id: 30,
					name: '30天内'
				}]

				education.unshift({
					id: 0,
					name: '不限'
				})
				experience.unshift({
					id: 0,
					name: '不限'
				})
				resumeTag.unshift({
					id: 0,
					name: '不限'
				})
				let gender = [{
					name: '不限',
					id: 0
				}, {
					name: '男',
					id: 1
				}, {
					name: '女',
					id: 2
				}]
				let wage = [{
						name: '不限',
						id: 0,
						min: '',
						max: '',
						select: true
					},
					{
						name: '1千以下',
						id: 1,
						min: 0,
						max: 1000,
						select: false
					},
					{
						name: '1千-2千',
						id: 2,
						min: 1000,
						max: 2000,
						select: false
					},
					{
						name: '2千-3千',
						id: 3,
						min: 2000,
						max: 3000,
						select: false
					},
					{
						name: '3千-5千',
						id: 4,
						min: 3000,
						max: 5000,
						select: false
					},
					{
						name: '5千-8千',
						id: 5,
						min: 5000,
						max: 8000,
						select: false
					},
					{
						name: '8千-1万2',
						id: 6,
						min: 8000,
						max: 12000,
						select: false
					},
					{
						name: '1万2-1万5',
						id: 7,
						min: 12000,
						max: 15000,
						select: false
					},
					{
						name: '1万5以上',
						id: 8,
						min: 15000,
						max: '',
						select: false
					}
				]
				this.tabbar.forEach(item => {
					if (item.name == '薪资待遇') item.list = wage
					if (item.name == '性别要求') item.list = gender
					if (item.name == '经验要求') item.list = experience
					if (item.name == '学历要求') item.list = education
					if (item.name == '年龄要求') item.list = age
					if (item.name == '简历标签') item.list = resumeTag
					if (item.name == '更新时间') item.list = updateTime
				})

				this.tabbar.map(item => {
					item.list = item.list.map((item1, index1) => {
						if (index1 == 0) {
							item1.check = true
						} else {
							item1.check = false
						}
						return item1
					})
					return item
				})
				this.$nextTick(() => {
					this.getMenuItemTop()
				})
			},
			// 点击左边的栏目切换
			async swichMenu(index) {
				if (this.arr.length == 0) {
					await this.getMenuItemTop();
				}
				if (index == this.current) return;
				this.scrollRightTop = this.oldScrollTop;
				this.isRightScroll = true
				this.$nextTick(function() {
					this.scrollRightTop = this.arr[index];
					this.current = index;
					this.leftMenuStatus(index);
				})
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({
						size: true
					}, res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
						resolve();
					}).exec();
				})
			},
			// 观测元素相交状态
			async observer() {
				this.tabbar.map((val, index) => {
					let observer = uni.createIntersectionObserver(this);
					// 检测右边scroll-view的id为itemxx的元素与right-box的相交状态
					// 如果跟.right-box底部相交，就动态设置左边栏目的活动状态
					observer.relativeTo('.right-box', {
						top: 0
					}).observe('#item' + index, res => {
						if (res.intersectionRatio > 0) {
							let id = res.id.substring(4);
							this.leftMenuStatus(id);
						}
					})
				})
			},
			// 设置左边菜单的滚动状态
			async leftMenuStatus(index) {
				this.current = index;
				// 如果为0，意味着尚未初始化
				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 获取右边菜单每个item到顶部的距离
			getMenuItemTop() {
				new Promise(resolve => {
					let selectorQuery = uni.createSelectorQuery();
					selectorQuery.selectAll('.class-item').boundingClientRect((rects) => {
						// 如果节点尚未生成，rects值为[](因为用selectAll，所以返回的是数组)，循环调用执行
						if (!rects.length) {
							setTimeout(() => {
								this.getMenuItemTop();
							}, 10);
							return;
						}
						rects.forEach((rect) => {
							// 这里减去rects[0].top，是因为第一项顶部可能不是贴到导航栏(比如有个搜索框的情况)
							this.arr.push(rect.top - rects[0].top);
							resolve();
						})
					}).exec()
				})
			},
			// 右边菜单滚动
			async rightScroll(e) {
				this.oldScrollTop = e.detail.scrollTop;
				if (this.arr.length == 0) {
					await this.getMenuItemTop();
				}
				if (this.timer) return;
				if (!this.menuHeight) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
				}
				if (this.isRightScroll) return this.isRightScroll = !this.isRightScroll
				setTimeout(() => { // 节流
					this.timer = null;
					// scrollHeight为右边菜单垂直中点位置
					let scrollHeight = e.detail.scrollTop + this.menuHeight / 2;
					for (let i = 0; i < this.arr.length; i++) {
						let height1 = this.arr[i];
						let height2 = this.arr[i + 1];
						// 如果不存在height2，意味着数据循环已经到了最后一个，设置左边菜单为最后一项即可
						if (!height2 || scrollHeight >= height1 && scrollHeight < height2) {
							this.leftMenuStatus(i);
							return;
						}
					}
				}, 10)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.u-wrap {
		display: flex;
		flex-direction: column;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}


	.u-tab-view {
		width: 200rpx;
		// height: 100%;
		background-color: #f3f5f8;
	}

	.u-tab-item {
		height: 110rpx;
		background: #F3F5F8;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		color: #000;
		line-height: 1;
	}

	.u-tab-item-active {
		position: relative;
		color: #3584E0;
	}

	.u-tab-item-active::before {
		content: '';
		width: 50rpx;
		height: 4rpx;
		position: absolute;
		top: 80rpx;
		background-color: #3584E0;
	}

	.right-box {
		background-color: rgb(250, 250, 250);
	}

	.page-view {
		padding: 16rpx;
		padding-bottom: 120rpx;
	}

	.class-item {
		margin-bottom: 30rpx;
		background-color: #fff;
		padding: 16rpx;
		border-radius: 8rpx;
	}

	.item-title {
		font-size: 32rpx;
		color: #000;
		font-weight: bold;
	}

	.item-container {
		display: flex;
		flex-wrap: wrap;
	}

	.thumb-box {
		background-color: #F3F5F8;
		border-radius: 20rpx;
		width: 234rpx;
		height: 65rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-right: 30rpx;
		margin-top: 18rpx;
		font-size: 26rpx;
		color: #2E2E2E;
		text-align: center;

		&--active {
			border: 2rpx solid #4b92e2;
			border-radius: 20rpx;
			background-color: #DEEEFC;
			color: #3584E0;
		}

		&--active-all {
			border: 2rpx solid #4b92e2;
			border-radius: 20rpx;
			background-color: #fff;
			color: #3584E0;
		}
	}

	.thumb-box:nth-child(2n) {
		margin-right: 0;
	}


	.item-menu-name {
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		width: 200rpx;
		padding: 0 20rpx;
	}
</style>